package com.unitd.base.module.consumer.controller;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.unitd.base.frame.plugin.module.dto.ResponseUtils;
import com.unitd.base.module.consumer.comm.annotation.IgnoreAuth;
import com.unitd.base.module.consumer.comm.annotation.LoginUser;
import com.unitd.base.module.consumer.comm.controller.ApiBaseAction;
import com.unitd.base.module.master.pojo.dto.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 作者: @author Harmon <br>
 *
 * @gitee https://gitee.com/fuyang_lipengjun/platform
 * 时间: 2017-08-11 08:32<br>
 * 描述: ApiIndexController <br>
 */
@Api(tags = "公司客戶信息")
@RestController
@RequestMapping("/api/agentUser")
public class ApiAgentUserController extends ApiBaseAction {

    //测试代码
    public static List<UserVO> agentUserEntities = Lists.newArrayList();

    static {
        UserVO agentUser1 = new UserVO();
        agentUser1.setUserId(10001L);
        agentUser1.setUserName("张三");
        agentUser1.setGender(1);
        agentUser1.setUserLevelId(1L);
        agentUser1.setMobile("13611111111");

        UserVO agentUser2 = new UserVO();
        agentUser2.setUserId(10002L);
        agentUser2.setUserName("李四");
        agentUser2.setGender(1);
        agentUser2.setUserLevelId(1L);
        agentUser2.setMobile("13622222222");
        agentUserEntities.add(agentUser1);
        agentUserEntities.add(agentUser2);
    }

    /**
     * 获取用户的公司客戶信息
     */
    @ApiOperation(value = "获取用户的公司客戶信息接口", response = Map.class)
    @PostMapping("list")
    @IgnoreAuth
    public Object list(@LoginUser UserVO loginUser) {
        //测试代码
        //Map<String, Object> param = Maps.newHashMap();
        //param.put("user_id", loginUser.getUserId());
//        List<UserVo> AgentUserEntities = AgentUserService.queryList(param);//测试代码
        return ResponseUtils.toResponsSuccess(agentUserEntities);
    }

    /**
     * 获取公司客戶信息的详情
     */
    @ApiOperation(value = "获取公司客戶信息的详情", response = Map.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "公司客戶ID", required = true, dataType = "Integer")})
    @PostMapping("detail")
    @IgnoreAuth
    public Object detail(Integer id, @LoginUser UserVO loginUser) {
        //测试代码
//        UserVo entity = AgentUserService.queryObject(id);
        UserVO entity = new UserVO();
        for (UserVO agentUser : agentUserEntities) {
            if (String.valueOf(agentUser.getUserId()).equals(String.valueOf(id))) {
                entity = agentUser;
                entity.setUserId(agentUser.getUserId());
                entity.setGender(agentUser.getGender());
                entity.setUserLevelId(agentUser.getUserLevelId());
                entity.setMobile(agentUser.getMobile());
            }
        }
        //判断越权行为
//        if (!entity.getUserId().equals(loginUser.getUserId())) {
//            return ResponseUtils.toResponsObject(403, "您无权查看", "");
//        }
        return ResponseUtils.toResponsSuccess(entity);
    }

    /**
     * 添加或更新公司客戶信息
     */
    @ApiOperation(value = "添加或更新公司客戶信息", response = Map.class)
    @PostMapping("save")
    @IgnoreAuth
    public Object save(@LoginUser UserVO loginUser) {
        JSONObject AgentUserJson = this.getJsonRequest();
        UserVO entity = new UserVO();
        if (null != AgentUserJson) {
            entity.setUserId(AgentUserJson.getLong("userId"));
            entity.setUserName(AgentUserJson.getString("username"));
            entity.setGender(AgentUserJson.getInteger("gender"));
            entity.setUserLevelId(AgentUserJson.getLong("user_level_id"));
            entity.setMobile(AgentUserJson.getString("mobile"));
        }
        //测试代码
        agentUserEntities.removeIf(agentUser -> String.valueOf(agentUser.getUserId()).equals(String.valueOf(entity.getUserId())));
        agentUserEntities.add(entity);
//        if (null == entity.getId() || entity.getId() == 0) {
//            entity.setId(null);
//            AgentUserService.save(entity);
//        } else {
//            AgentUserService.update(entity);
//        }
        return ResponseUtils.toResponsSuccess(entity);
    }

    /**
     * 删除指定的公司客戶信息
     */
    @ApiOperation(value = "删除指定的公司客戶信息", response = Map.class)
    @PostMapping("delete")
    @IgnoreAuth
    public Object delete(@LoginUser UserVO loginUser) {
        JSONObject jsonParam = this.getJsonRequest();
        Long id = jsonParam.getLongValue("id");
        //测试代码
        agentUserEntities.removeIf(agentUser -> String.valueOf(agentUser.getUserId()).equals(String.valueOf(id)));
        //UserVo entity = AgentUserService.queryObject(id);
        //判断越权行为
//        if (!entity.getUserId().equals(loginUser.getUserId())) {
//            return ResponseUtils.toResponsObject(403, "您无权删除", "");
//        }
//        AgentUserService.delete(id);
        return ResponseUtils.toResponsSuccess("");
    }
}